<?php
//团购控制器
class Controller_App_Tuan extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/tuan/detail";
        parent::__construct($req, $res);
    }


    // 开团 / 参团
    public function action_create ()
    {
        $post = $this->request->post();

        $arrBuyInfo     = array();
        $tuanId         = Arr::get($post, 'tuanId');
        $goodsIds       = Arr::get($post, 'goodsId');
        $goodsAttrIds   = Arr::get($post, 'goodsAttrId');
        $goodsPrices    = Arr::get($post, 'goodsPrice');
        $goodsQuantities= Arr::get($post, 'goodsQuantity');
        $index = 0;
        foreach ($goodsIds as $goodsId)
        {
            $arrBuyInfo[] = array(  
                'goodsId' => $goodsIds[$index],
                'goodsAttrId'   => $goodsAttrIds[$index],
                'goodsPrice'    => $goodsPrices[$index],
                'goodsQuantity' => $goodsQuantities[$index],
            );

            $index++; 
        }

        $coinMoney = Arr::get($post, 'coinMoney', 0);
        $ticketId  = Arr::get($post, 'ticketId', 0);
        
        $addressId = Arr::get($post, 'addressId', 0);
        $deliverId = Arr::get($post, 'deliverId', 0);

        $paymentId = Arr::get($post, 'paymentId', 0);

        $comments  = Arr::get($post, 'comments', '');

        $invoiceInfo = array(
            'invoiceType'    => Arr::get($post, 'invoiceType', 0),
            'invoiceContent' => Arr::get($post, 'invoiceContent', 0),
            'invoiceTitle'   => Arr::get($post, 'invoiceTitle', ''),
        );

        try {

            // 创建订单
            $orderNo = App_Order::create($arrBuyInfo, $coinMoney, $ticketId, $addressId, $deliverId, $paymentId, $invoiceInfo, $comments, 'tuan');

            // 生成/初始化 团购记录
            $tuanId  = App_Tuan::addOrder($orderNo, $tuanId);

            $this->request->redirect('/order/detail/' . $orderNo);
        } 
        catch (Exception $e)
        {
            //
            echo $e->getMessage();exit;
        }
    }

    // 团购详情
    public function action_tipstext ()
    {
        $this->template->set_filename('smarty:app/tuan/tipstext');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '团购流程', 'share', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

	// 团购详情
    public function action_detail ($nid)
    {
        $detail    = App_Order_Tuan::detail($nid);
        $hasJoined = App_Order_Tuan::hasJoined($this->_uid, $nid);
        $progress  = App_Order_Tuan::progressStepStyle($detail, $this->_uid);
        $goods     = App_Goods::detail($detail['goodsId']);

        $wants = array();
        $wantsCounts = $detail['wantPersons'];
        while ($wantsCounts > 0)
        {
            $wantsCounts--;
            $wants[] = 1;
        }
        $detail['wants'] = $wants;

        $this->template->set('title', '团购详情');
        $this->template->set('detail', $detail);
        $this->template->set('progress', $progress);
        $this->template->set('userJoinedTuan', $hasJoined);
        $this->template->set('goods', $goods);
        $this->template->set('tuan', $goods);
        $this->template->set('shareTitle', App_Util::buildShareContent($goods['name']));
        $this->template->set('shareType', 'tuan_detail');
//        $this->template->set('shareDesc', App_Util::buildShareContent($goods['shortDesc']));
        $this->template->set('buyTuanId', $nid);
        $this->template->set('nowTime', date("Y-m-d H:i:s", time()));
        $this->template->set('timeLeftSeconds', $this->tuanTimeLeftSeconds($detail['startTime']));

        if (site::wechat())
        {
            // 初始化团购分享参数
            $thumbsrc = $goods['thumbsrc'];
            if (! preg_match('/^http:\/\//', $thumbsrc))
                $thumbsrc = url::site($thumbsrc, 'http', FALSE);

            $this->initWechatShare("邀请参团",  $thumbsrc, $goods['name']);
            $this->template->set('include_wechat_share', TRUE);
        }

        $title_json  = App_Util::buildClientTitleJson('back', 'block', '团购详情', 'share', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    private function tuanTimeLeftSeconds($intStartTime)
    {
        $endTime  = $intStartTime + 86400;
        $currentT = time();
        if ($endTime >= $currentT)
            return $endTime - $currentT;
        else
            return 0;
    }
}
